package com.jyb.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jyb.enumeration.ShanghaiDistrict;
import com.jyb.model.Jxjh;
import com.jyb.model.MyOrder;
import com.jyb.model.QueryLog;
import com.jyb.model.User;
import com.jyb.util.CheckUserUtil;
import com.jyb.util.CqjhMtUtil;
import com.jyb.util.MD5Util;
import com.jyb.util.StringUtils;
import com.jyb.util.UUIDUtils;
import com.jyb.util.ValidateKit;
import com.jyb.util.YzmUtil;
import com.jyb.util.capture.CbxxUtil;
import com.jyb.util.capture.CqjhUtil;
import com.jyb.util.capture.DzxdUtil;
import com.jyb.util.capture.GqxhUtil;
import com.jyb.util.capture.HgfxUtil;
import com.jyb.util.capture.YpcdUtil;
import com.jyb.vo.BaseVo;
import com.jyb.vo.CbxxVo;
import com.jyb.vo.CqjhVo;
import com.jyb.vo.DzxdVo;
import com.jyb.vo.GqxhVo;
import com.jyb.vo.HgfxVo;
import com.jyb.vo.UserVo;
import com.jyb.vo.YpcdVo;

public class ApiController extends Controller{

	private UserVo getUserVoByToken(String token){
		String username=CheckUserUtil.getUsername(token);
		User user=User.dao.getUserByCellphone(username);
		if(user!=null){
			return User.getUserVo(user);
		}
		return null;
	}
	
	public void index(){
		String m=getPara("m");
		if("S01".equals(m)){
			login();
		}else if("S02".equals(m)){
			register();
		}else if("S03".equals(m)){
			sms();
		}else if("S04".equals(m)){
			isCellphoneExit();
		}else if("J01".equals(m)){
			cbxx();
		}else if("J02".equals(m)){
			ypcd();
		}else if("J03".equals(m)){
			jxjh();
		}else if("J04".equals(m)){
			hgfx();
		}else if("J05".equals(m)){
			cqjhcxtj();
		}else if("J06".equals(m)){
			cqjh();
		}else if("J07".equals(m)){
			gqxh();
		}else if("J08".equals(m)){
			dzxd();
		}else if("J09".equals(m)){
			myOrder();
		}else{
			BaseVo baseVo=new BaseVo();
			baseVo.setFlag(false);
			baseVo.setMsg("参数错误！");
			renderJson(baseVo);
		}
	}
	
	/**
	 * 登录接口
	 */
	private void login(){
		BaseVo baseVo=new BaseVo();
		String cellphone=getPara("cellphone");
		String password=getPara("password");
		
		if(StringUtils.isEmpty(cellphone)){
			baseVo.setFlag(false);
			baseVo.setMsg("手机号不能为空！");
		}else if(StringUtils.isEmpty(password)){
			baseVo.setFlag(false);
			baseVo.setMsg("密码不能为空！");
		}else{
			User user=User.dao.getUserByCellphoneAndPassword(cellphone, password);
			if(user!=null){
				String token=CheckUserUtil.encode(cellphone);
				baseVo.setFlag(true);
				baseVo.setMsg("登录成功！");
				baseVo.setData(token);
			}else{
				baseVo.setFlag(false);
				baseVo.setMsg("账号或密码错误！");
			}
		}
		
		renderJson(baseVo);
	}
	
	/**
	 * 注册接口
	 */
	private void register(){
		BaseVo baseVo=new BaseVo();
		String cellphone=getPara("cellphone");
		String password=getPara("password");
		String smsyzm=getPara("smsyzm");
		
		if(!ValidateKit.isMobile(cellphone)){
			baseVo.setFlag(false);
			baseVo.setMsg("手机号格式错误！");
		}else if(StringUtils.isEmpty(password)){
			baseVo.setFlag(false);
			baseVo.setMsg("密码不能为空！");
		}else if(StringUtils.isEmpty(smsyzm)){
			baseVo.setFlag(false);
			baseVo.setMsg("短信验证码不能为空！");
		}else if(!YzmUtil.isValid(cellphone, smsyzm)){
			baseVo.setFlag(false);
			baseVo.setMsg("短信验证码错误！");
		}else{
			User userTemp=User.dao.getUserByCellphone(cellphone);
			if(userTemp!=null){
				baseVo.setFlag(false);
				baseVo.setMsg("该手机号已存在！");
			}else{
				User user=new User();
				user.set("id", UUIDUtils.getUUID());
				user.set("createTime", new Date());
				user.set("cellphone",cellphone);
				user.set("password", MD5Util.MD5(password));
				user.set("nickname", cellphone);
				user.save();
				
				String token=CheckUserUtil.encode(cellphone);
				baseVo.setFlag(true);
				baseVo.setMsg("注册成功！");
				baseVo.setData(token);
			}
		}
		
		renderJson(baseVo);
	}
	
	/**
	 * 短信接口
	 */
	private void sms(){
		String cellphone=getPara("cellphone");
		BaseVo baseVo=new BaseVo();
		if(ValidateKit.isMobile(cellphone)){
			User user=User.dao.getUserByCellphone(cellphone);
			if(user==null){
				baseVo=YzmUtil.sendYzm(cellphone);
			}else{
				baseVo.setFlag(false);
				baseVo.setMsg("该手机号已被注册!");
			}
		}else{
			baseVo.setFlag(false);
			baseVo.setMsg("手机号格式错误!");
		}
		renderJson(baseVo);
	}
	
	/**
	 * 手机号是否有效
	 */
	private void isCellphoneExit(){
		String cellphone=getPara("cellphone");
		BaseVo baseVo=new BaseVo();
		if(ValidateKit.isMobile(cellphone)){
			User user=User.dao.getUserByCellphone(cellphone);
			if(user==null){
				baseVo.setFlag(true);
				baseVo.setMsg("该手机号不存在!");
				baseVo.setData(false);
			}else{
				baseVo.setFlag(true);
				baseVo.setMsg("该手机号已存在!");
				baseVo.setData(true);
			}
		}else{
			baseVo.setFlag(false);
			baseVo.setMsg("手机号格式错误!");
		}
		renderJson(baseVo);
	}
	
	/**
	 * 船舶信息
	 */
	private void cbxx(){
		
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String cm=getPara("cm");
		
		List<CbxxVo> vos=new ArrayList<CbxxVo>();
		if(!StringUtils.isEmpty(cm)){
			cm=cm.trim();
			vos=CbxxUtil.capture(cm);
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "0", cm);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	/**
	 * 预配舱单
	 */
	private void ypcd(){
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String tdh=getPara("tdh");
		
		List<YpcdVo> vos=new ArrayList<YpcdVo>();
		if(!StringUtils.isEmpty(tdh)){
			tdh=tdh.trim();
			vos=YpcdUtil.capture(tdh);
		}
		
		if(userVo!=null){
			for (YpcdVo ypcdVo : vos) {
				MyOrder.dao.updateMyOrder(userVo.getId(), tdh, ypcdVo.getCm(), ypcdVo.getCc());
			}
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "1", tdh);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	/**
	 * 进箱计划
	 */
	private void jxjh(){
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String cm=getPara("cm");
		
		List<Jxjh> vos=new ArrayList<Jxjh>();
		if(!StringUtils.isEmpty(cm)){
			cm=cm.trim();
			vos=Jxjh.dao.findListByCm(cm, null);
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "0", cm);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	/**
	 * 海关放行
	 */
	private void hgfx(){
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String tdh=getPara("tdh");
		List<HgfxVo> vos=new ArrayList<HgfxVo>();
		if(!StringUtils.isEmpty(tdh)){
			tdh=tdh.trim();
			vos=HgfxUtil.capture(tdh);
		}
		
		if(userVo!=null){
			for (HgfxVo hgfxVo : vos) {
				MyOrder.dao.updateMyOrder(userVo.getId(), tdh, hgfxVo.getCm(), hgfxVo.getHc());
			}
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "1", tdh);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	/**
	 * 船期计划查询条件
	 */
	private void cqjhcxtj(){
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(CqjhMtUtil.getMts());
		
		renderJson(baseVo);
		
	}
	
	/**
	 * 船期计划
	 */
	private void cqjh(){
		
		String mt=getPara("mt");
		String value=getPara("value");
		
		List<CqjhVo> vos=new ArrayList<CqjhVo>();
		BaseVo baseVo=new BaseVo();
		
		if(!StringUtils.isEmpty(value)&&!StringUtils.isEmpty(mt)){
			
			value=value.trim();
			ShanghaiDistrict district=ShanghaiDistrict.of(mt);
			vos=CqjhUtil.capture(district, value);
			
			baseVo.setFlag(true);
			baseVo.setMsg("查询成功！");
			baseVo.setData(vos);
		}else{
			baseVo.setFlag(false);
			baseVo.setMsg("查询参数错误!");
		}
		
		renderJson(baseVo);
		
	}
	
	/**
	 * 港区箱货
	 */
	private void gqxh(){
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String tdh=getPara("tdh");
		String mt=getPara("mt");
		
		List<GqxhVo> vos=new ArrayList<GqxhVo>();
		if(!StringUtils.isEmpty(tdh)&&!StringUtils.isEmpty(mt)){
			tdh=tdh.trim();
			if("auto".equals(mt)){
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.WY, tdh));
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.WE, tdh));
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.WS, tdh));
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.WW, tdh));
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.YY, tdh));
				vos.addAll(GqxhUtil.capture(ShanghaiDistrict.YS, tdh));
			}else{
				ShanghaiDistrict district=ShanghaiDistrict.of(mt);
				vos=GqxhUtil.capture(district, tdh);
			}
		}
		
		if(userVo!=null){
			for (GqxhVo gqxhVo : vos) {
				MyOrder.dao.updateMyOrder(userVo.getId(), tdh, "", "");
				MyOrder.dao.updateMyOrderMt(userVo.getId(), tdh, gqxhVo.getShanghaiDistrict());
				
			}
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "1", tdh);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	/**
	 * 电子箱单
	 */
	private void dzxd(){
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		String tdh=getPara("tdh");
		List<DzxdVo> vos=new ArrayList<DzxdVo>();
		
		if(!StringUtils.isEmpty(tdh)){
			tdh=tdh.trim();
			vos=DzxdUtil.capture(tdh);
		}
		
		if(userVo!=null){
			for (DzxdVo vo : vos) {
				MyOrder.dao.updateMyOrder(userVo.getId(), tdh, vo.getCm(), vo.getHc());
			}
		}
		
		if(userVo!=null&&vos.size()>0){
			QueryLog.dao.addOrUpdate(userVo.getId(), "1", tdh);
		}
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setMsg("查询成功！");
		baseVo.setData(vos);
		
		renderJson(baseVo);
	}
	
	private void myOrder(){
		BaseVo baseVo=new BaseVo();
		
		String token=getPara("token");
		UserVo userVo=getUserVoByToken(token);
		if(userVo==null){
			baseVo.setFlag(false);
			baseVo.setMsg("参数错误！");
		}else{
			List<Record> records=MyOrder.dao.getNewest(30, userVo.getId());
			baseVo.setFlag(true);
			baseVo.setMsg("查询成功！");
			baseVo.setData(records);
		}
		renderJson(baseVo);
	}
}
